package com.api.aiyh_pcn.async_organization;

import aiyh.utils.zwl.common.ToolUtil;
import com.ibm.icu.text.SimpleDateFormat;
import km.org.apache.poi.hssf.usermodel.HSSFCellStyle;
import km.org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import weaver.aiyh_pcn.async_organization.model.Department;
import weaver.aiyh_pcn.async_organization.model.Employee;
import weaver.aiyh_pcn.async_organization.model.Position;
import weaver.aiyh_pcn.async_organization.result.GetOrganizationResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author EBU7-dev1-ayh
 * @create 2021/8/8 0008 16:06
 * 导出api请求数据的表格
 */

@Path("/asyncOrganization")
public class ExportExcel {

	@GET
	@Path("/exportExcel")
	@Produces(MediaType.APPLICATION_OCTET_STREAM)
	public Response exportExcel(@Context HttpServletRequest request, @Context HttpServletResponse response) {
		ToolUtil toolUtil = new ToolUtil();
		GetOrganizationResult getOrganizationResult = new GetOrganizationResult();
		List<Employee> employeeList = getOrganizationResult.getEmployeeList();
		SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook();
		CellStyle cellStyle = sxssfWorkbook.createCellStyle();
		// 设置单元格居中对齐
		cellStyle.setAlignment(HorizontalAlignment.forInt(HSSFCellStyle.ALIGN_CENTER));
		Font font = sxssfWorkbook.createFont();
		font.setColor(HSSFColor.RED.index);
		cellStyle.setFont(font);
		Sheet sheet = sxssfWorkbook.createSheet("人员信息");
		Row desc = sheet.createRow(0);
		sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 13));
		Cell cell2 = desc.createCell(0);
		cell2.setCellValue("员工信息表，同步数据时，按照岗位id进行排序同步！");
		cell2.setCellStyle(cellStyle);
		Row th = sheet.createRow(1);
		th.createCell(0).setCellValue("员工ID");
		th.createCell(1).setCellValue("名（英文）");
		th.createCell(2).setCellValue("姓（英文）");
		th.createCell(3).setCellValue("中文姓名");
		th.createCell(4).setCellValue("英文名");
		th.createCell(5).setCellValue("员工编号");
		th.createCell(6).setCellValue("岗位ID");
		th.createCell(7).setCellValue("部门ID");
		th.createCell(8).setCellValue("部门名称");
		th.createCell(9).setCellValue("成本中心编码");
		th.createCell(10).setCellValue("邮箱");
		th.createCell(11).setCellValue("公司实体");
		th.createCell(12).setCellValue("电话号");
		th.createCell(13).setCellValue("座机号");
		try {
			for (int i = 0; i < employeeList.size(); i++) {
				Employee e = employeeList.get(i);
//				toolUtil.writeErrorLog("item数据库：" + e);
				if (e.getJOBCODEID() == null || e.getJOBCODEID() == 0){
					toolUtil.writeErrorLog("数据出现错误：" + e);
					continue;
				}
				Row row = sheet.createRow(i + 2);
				row.createCell(0).setCellValue(e.getUserID());
				row.createCell(1).setCellValue(e.getFIRSTNAMEEN());
				row.createCell(2).setCellValue(e.getLASTNAMEEN());
				row.createCell(3).setCellValue(e.getUSERNAMECN());
				row.createCell(4).setCellValue(e.getPreferred_Name());
				row.createCell(5).setCellValue(e.getUSERCODE());
				row.createCell(6).setCellValue(e.getJOBCODEID());
				row.createCell(7).setCellValue(e.getDEPARTMENTID());
				row.createCell(8).setCellValue(e.getDEPARTMENTNAME());
				row.createCell(9).setCellValue(e.getCOSTCENTERCODE());
				row.createCell(10).setCellValue(e.getEMAIL());
				row.createCell(11).setCellValue(e.getORGANIZATION());
				row.createCell(12).setCellValue(e.getMOBILENO());
				row.createCell(13).setCellValue(e.getTEL());
			}
		}catch (Exception e){
			toolUtil.writeErrorLog("出现异常："+e);
		}

		List<Department> departmentList = getOrganizationResult.getDepartmentList();
		Sheet depSheet = sxssfWorkbook.createSheet("部门信息");
		Row depdesc = depSheet.createRow(0);
		depSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 2));
		Cell cell1 = depdesc.createCell(0);
		cell1.setCellValue("部门信息表，同步数据时按照该上级部门id进行排序同步！红色字体为根部门！");
		cell1.setCellStyle(cellStyle);
		Row depTh = depSheet.createRow(1);
		depTh.createCell(0).setCellValue("部门ID");
		depTh.createCell(1).setCellValue("部门名称");
		depTh.createCell(2).setCellValue("上级部门");
		for (int i = 0; i < departmentList.size(); i++) {
			Row row = depSheet.createRow(i + 2);
			Department d = departmentList.get(i);
			row.createCell(0).setCellValue(d.getDEPARTMENTID());
			row.createCell(1).setCellValue(d.getDEPARTMENTNAME());
			Cell cell = row.createCell(2);
			cell.setCellValue(d.getPARENTDEPARTMENDID());
			if (d.getPARENTDEPARTMENDID() == 0) {
				cell.setCellStyle(cellStyle);
			}
		}

		List<Position> positionList = getOrganizationResult.getPositionList();
		Sheet posSheet = sxssfWorkbook.createSheet("职位信息");
		Row posdesc = posSheet.createRow(0);
		posSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 3));
		Cell cell3 = posdesc.createCell(0);
		cell3.setCellValue("职位信息表，职位同步过程中以上级岗位id升序排序进行同步！");
		cell3.setCellStyle(cellStyle);
		Row posTh = posSheet.createRow(1);
		posTh.createCell(0).setCellValue("岗位编号");
		posTh.createCell(1).setCellValue("岗位编号");
		posTh.createCell(2).setCellValue("岗位描述");
		posTh.createCell(3).setCellValue("上级岗位ID");
		for (int i = 0; i < positionList.size(); i++) {
			Row row = posSheet.createRow(i + 2);
			Position p = positionList.get(i);
			row.createCell(0).setCellValue(p.getJOBCODEID());
			row.createCell(1).setCellValue(p.getJOBCODE());
			row.createCell(2).setCellValue(p.getJOBFUNCTION());
			row.createCell(3).setCellValue(p.getSUPERIORJOBCODEID());
		}
		SimpleDateFormat newsdf = new SimpleDateFormat("yyyy-MM-dd-HHmmss");
		String date = newsdf.format(new Date());
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		try {
			sxssfWorkbook.write(bos);
			return Response.ok(bos.toByteArray(), "application/msexcel;charset=UTF-8")
					.header("Content-Disposition", "attachment;filename=\""
							+ new String(("CMS人员同步接口数据" + date + ".xlsx").getBytes("GBK"),
							"ISO8859_1") + "\"")
					.build();
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
}
